<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

// ******************调用微博相关文件*****************************
			session_start();  //任何时候，页面使用session，开头必须有此句声明。
			require_once(APP_PATH."config.php" );
			require_once(APP_PATH."weibooauth.php" );
// ******************调用微博相关文件*****************************
			
class main {
	function __construct (){
		$this->url = getUrl();
		$action = $this->url['action'];
		if($action == 'new'){
			$this->addReview();
		}elseif($action == 'edit'){
			$this->editReview();
		}elseif($action == 'reply'){
			$this->replyAction();
		}elseif($action == 'vote'){
			$this->voteAction();
		}elseif(is_numeric($action)){
			$this->showAction();
		}else{
			$this->defaultAction();
		}
		exit();
	}

	function defaultAction(){
		exit('ss');
	}

	function showAction(){
		require_once(APP_PATH."model/review.php" );
		$db  = getDB();
		$data = new reviewModel($db);
		$id = $this->url['action'];
		$review = $data->getReview($id);
		empty($review) && pageError();
		$movie = $data->getMovie($review['mid']);		
		$movie['mid'] = $movie['id'];
		$movie['mtitle'] = $movie['title'];

		$user = $data->getUser($review['uid']);

		$review['addtime'] = date('Y-m-d H:i',$review['addtime']);
		$user['avatars'] = empty($user['avatars']) ? 'default.gif' : $user['avatars'];
		$review['rating'] = $review['rating'] / 2;
		$review['rtitle'] = $review['title'];
		

		if($this->url['params']['1'] == 'page'){
			$pagetemp = $this->url['params']['2'];		
		}
		$param['table'] = 'movie_review_reply ';
		$param['field'] = ' * ';
		$param['where'] = " rid = '$id' ";
		$param['order'] = 'addtime DESC ';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '15';
		$param['pageUrl'] = '/review/'.$id.'/page/';
		$param['pageTotal']	= $db->getTotal($param);		
		$replys = $db->getList($param);

		foreach($replys as $k => $v ){
			$uids .= "'".$v['uid']."',";
		}
		if($uids){
			$membertmp = $data->getMembers(substr($uids,0,-1));
			foreach($membertmp as $k => $v ){
				$members[$v['uid']] =  $v;
			}
		}
		foreach($replys as $k => $v ){
			$uid = $v['uid'];
			$replys[$k]['addtime'] = date("Y-m-d",$v['addtime']);
			$replys[$k]['username'] = $members[$uid]['username'];
			$replys[$k]['avatars'] = empty($members[$uid]['avatars']) ? 'default.gif' : $members[$uid]['avatars'];
			$replys[$k]['contents'] = csubstr(html2Txt($v['contents']),120);
		}

		//de($replys);

		
		$tpl = new Template("review/main.htm");
		$tpl->assign($movie);
		$tpl->assign($review);
		$tpl->assign('replys',$replys);
		$movie['total'] = $param['pageTotal'];
		$tpl->assign("pages", makePage($param));
		$tpl->assign($user);
		$tpl->assign(assignUser());
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function addReview(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$movie = $data->getMovie($id);
		//de($movie);
		empty($movie) && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$uid = $user['uid'];
		$review = $data->isPostReview($id,$uid);
		//de($review);
		
		if(isPost()){			
			$title = htmlClear(trim($_POST['title']));

			$rating = trim($_POST['rating'])  * 2;
			$content = txt2Html($_POST['content']);	
			
			if (!is_numeric($rating)){
				message('您的评分数正确',2);
			}

			if ($rating == '0'){
				message('请选择您的评分',2);
			}

			if (!checkLen($title,6,200)){
				message('您输入的标题长度不合格',2);
			}
			

			global $timestamp,$onlineip;		
			$review_id = $data->addReview($id,$uid,$rating,$title,$content,$onlineip,$timestamp);
				
				
			
			//***********增加： 新增review时同步微博内容××××××××××××××××××××××××××××××××××××××××××××
			//session_start();  //任何时候，页面使用session，开头必须有此句声明。
			//require_once(APP_PATH."config.php" );
			//require_once(APP_PATH."weibooauth.php" );
			if ($_POST['checkbox'] =="check")
			{
			$mtitle=$_POST['mtitle'];    //电影标题
			$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
			
			$url = "http://".$_SERVER['SERVER_NAME']."/review/".$review_id;  //url在新浪的长度是10个中文字符
			$weibo= "我的影评 “".$mtitle.">".$title."” :".$content;
		    $weibo= cut_str($weibo, 128);   //这函数的中文字符数量，新浪极限是138, 超链接占9， 因此这里截取 128
			$weibo = $weibo."-".$url;			
			//echo $weibo; exit;//
			$rr = $c->update($weibo);
            }
			//***********增加： 新增review时同步微博内容××××××××××××××××××××××××××××××××××××××××××××
			

			
			if(!empty($review_id)){
				$data->updateMovieReviewNum($id,$rating);

				//如果评论为空。增加看过
				$is_add_comment = $data->isPostComment($id,$uid);
				if(empty($is_add_comment)){				
					$add_collect = $data->addComment($id,$uid,$rating,'1','',$onlineip,$timestamp);
				}

				message('','/review/'.$review_id);
			}else{
				message('发表影评失败，请重新发表。');
			}
		}else{
			if($review){
				$tpl = new Template("review/edit.htm");
				$review['rid'] = $review['id'];
				$review['rating'] = $review['rating'] / 2;
				$review['rTitle'] = $review['title'];

				$review['contents'] = str_replace("<br />","",$review['contents']);
				$tpl->assign($review);
			}else{
				$tpl = new Template("review/new.htm");
			}
			
			$movie['logintip'] = $user['logintip'];
			$tpl->assign($movie);
			$tpl->assign(assignUser());
			$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
			$tpl->output();
		}
	}
	
	function editReview(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$isreview = $data->getReview($id);
		empty($isreview) && pageError();
		
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$uid = $user['uid'];
		$mid = $isreview['mid'];
		$review = $data->isPostReview($mid,$uid);
		//de($review);
		if(empty($review)){
			message('您未对此影片发表影评');
		}
		
		if(isPost()){			
			$title = htmlClear(trim($_POST['title']));
			$rating = trim($_POST['rating'])  * 2;
			$content = txt2Html($_POST['content']);			
			if (!is_numeric($rating)){
				message('您的评分数不正确');
			}
			if (!checkLen($title,6,200)){
				message('您输入的标题长度不合格');
			}
				
			$review_id = $data->editReview($id,$rating,$title,$content);
			
			
			
			
			//*********** 修改review时同步微博内容×××××××××××××××××××××××××××××××××××××××××××
			if ($_POST['checkbox'] =="check")
			{
			$mtitle=$_POST['mtitle'];    //电影标题
			$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
			$url = "http://".$_SERVER['SERVER_NAME']."/review/".$id;  //url在新浪的长度是10个中文字符
			$weibo= "我的影评 “".$mtitle.">".$title."” :".$content;
		    $weibo= cut_str($weibo, 128);   //这函数的中文字符数量，新浪极限是138, 超链接占9， 因此这里截取 129
			$weibo = $weibo."-".$url;
			//echo "<br />". $weibo."<br />";
			//echo "长度：".strlen($weibo)."<br />";			
			$rr = $c->update($weibo);
			//var_dump($rr);
			//exit;
			}
			//*********** 修改review时同步微博内容××××××××××××××××××××××××××××××××××××××××××××××
			
			
			
			
			if(!empty($review_id)){
				message('','/review/'.$id);
			}else{
				message('修改影评失败。');
			}
		}else{
			exit();
		}		
		
	}

	function replyAction(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$id = intval(trim($id));

		$review = $data->getReview($id);
		empty($review) && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$uid = $user['uid'];
		if(isPost()){
			$content = htmlClear(trim($_POST['content']));	
			$mid = $review['mid'];
			if (!checkLen($content,3,5000)){
				message('您输入的内容太长或太短。');
			}
			global $timestamp,$onlineip;		
			$is_reply = $data->addReviewReply($id,$mid,$uid,$content,$onlineip,$timestamp);
			
			
			
			//***********增加： 回应同步到微博××××××××××××××××××××××××××××××××××××××××××××
			if ($_POST['checkbox'] =="check")
			{
			$mtitle=$_POST['mtitle'];    //电影标题
			$title=$_POST['title'];      //影评标题
			$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
			
			$url = "http://".$_SERVER['SERVER_NAME']."/review/".$id;  //url在新浪的长度是10个中文字符
			$weibo="回应影评 “".$mtitle." > ".$title."“ : ". $content;
		    $weibo= cut_str($weibo, 128);   //这函数的中文字符数量，新浪极限是138, 超链接占9， 因此这里截取 128
			$weibo = $weibo."-".$url;			
			$rr = $c->update($weibo);
            }
			//***********增加： 回应同步到微博×××××××××××××××××××××××××××××××××××××××××××
			
			
			
			if(!empty($is_reply)){
				$data->updateReviewReplyNum($id);
				message('','/review/'.$id);
			}else{
				message('回应影评失败，请重新发表。');
			}
		}else{
			pageError();
		}		
		
	}

	function voteAction(){
		require_once(APP_PATH."model/accounts/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['params']['1'];
		$type = $this->url['params']['2'];
		if($type == 'support'){
			$table = 'support';
		}elseif($type == 'against'){
			$table = 'against';
		}else{
			exit('系统错误');
		}
		!is_numeric($id) && exit('没有此影评');

		$review = $data->getReview($id);
		empty($review) && exit('没有此影评');
		$user = getMember();
		empty($user) && exit('nologin');
		
		$isVote = cookieget('review_'.$id);
		if(!empty($isVote)){
			exit('您已对此影评投过票');
		}
		$updateVote = $data->updateReviewSupport($id,$table);
		if(!empty($updateVote)){
			cookieset('review_'.$id,'1');
			exit('ok');
		}else{
			exit('此影评投票失败');
		}
		
	}

}
?>
